package com.ecoolex.ying.space.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.OutsourcingGoodsWithBLOBs;
import com.ecoolex.ying.space.entity.OutsourcingPackaged;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.WholeHouseCase;
import com.ecoolex.ying.space.pojo.QueryOutsourcingGoodsAndPackagedDTO;
import com.ecoolex.ying.space.pojo.QueryWholeHouseCaseAndUserWorksDTO;
import com.ecoolex.ying.space.ws.OutsourcingGoodsService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type = Type.User, description = "外购商品")
public class OutsourcingGoodsGraphql {
	
	@Reference
	private OutsourcingGoodsService outsourcingGoodsService;
	
	@Autowired
	private StorageService storageService;
	
	@TypeField(type = OutsourcingGoodsWithBLOBs.class, field = "wholeHouseCaseAndUserWorks", description = "关联的案例或者作品列表")
	public List<QueryWholeHouseCaseAndUserWorksDTO> wholeHouseCaseAndUserWorks(@Source OutsourcingGoodsWithBLOBs source,@NonNull Integer size){
		return outsourcingGoodsService.listByOutsourcingGoodsId(source.getOutsourcingGoodsId(), size);
	}
	
	@TypeField(type = QueryWholeHouseCaseAndUserWorksDTO.class, field = "image", description = "案例或者作品图片")
	public StorageInfo image(@Source QueryWholeHouseCaseAndUserWorksDTO source) {
		return storageService.get(source.getImage());
	}
	
	@TypeField(type = OutsourcingGoodsWithBLOBs.class, field = "coverImage", description = "列表图片")
	public StorageInfo coverImage(@Source OutsourcingGoodsWithBLOBs source) {
		return storageService.get(source.getCoverImage());
	}
	
	@TypeField(type = OutsourcingGoodsWithBLOBs.class, field = "outsourcingPackagedList", description = "商品搭配关联套餐列表")
	public Pagination<OutsourcingPackaged> outsourcingPackagedList(@Source OutsourcingGoodsWithBLOBs source,@NonNull Page page){
		return outsourcingGoodsService.queryOutsourcingPackagedByGoodsId(source.getOutsourcingGoodsId(), page);
	}
	
	@TypeField(type = OutsourcingGoodsWithBLOBs.class, field = "images", description = "商品图片")
	public List<StorageInfo> images(@Source OutsourcingGoodsWithBLOBs source) {
		JSONArray array = JSONArray.parseArray(source.getImages());
		List<String> storageIdLsit = array.toJavaList(String.class);
		return storageIdLsit.stream().map(storageId -> storageService.get(storageId)).collect(Collectors.toList());
	}
	
	@TypeField(type = QueryOutsourcingGoodsAndPackagedDTO.class, field = "coverImage", description = "列表图片")
	public StorageInfo goodsAndPackagedCoverImage(@Source QueryOutsourcingGoodsAndPackagedDTO source) {
		return storageService.get(source.getCoverImage());
	}
	
	@QueryField(field = "outsourcingGoods", description = "外购商品详情")
	public OutsourcingGoodsWithBLOBs get(@NonNull Integer outsourcingGoodsId) {
		return outsourcingGoodsService.getOutsourcingGoodsByIdForH5(outsourcingGoodsId);
	}
	
	@QueryField(field = "outsourcingGoodsList", description = "外购商品列表")
	public Pagination<OutsourcingGoodsWithBLOBs> query(Integer categoryId,String name,@NonNull Sorting[] sorts, @NonNull Page page){
		return outsourcingGoodsService.queryOutsourcingGoodsForH5(categoryId,name, sorts, page);
	}
	
	@QueryField(field = "outsourcingGoodsAndPackagedList", description = "外购商品和套餐列表")
	public Pagination<QueryOutsourcingGoodsAndPackagedDTO> query(String name, @NonNull Page page){
		return outsourcingGoodsService.queryOutsourcingGoodsAndPackaged(name, page);
	}
	
	@QueryField(field = "queryWholeHouseCaseByOutsourcingGoodsId", description = "根据商品Id查询案例列表")
	public Pagination<WholeHouseCase> queryWholeHouseCase(@NonNull Integer outsourcingGoodsId,@NonNull Page page){
		return outsourcingGoodsService.queryWholeHouseCase(outsourcingGoodsId, page);
	}
	
	@QueryField(field = "queryUserWorksByOutsourcingGoodsId", description = "根据商品Id查询作品列表")
	public Pagination<UserWorks> queryUserWorks(@NonNull Integer outsourcingGoodsId,@NonNull Page page){
		return outsourcingGoodsService.queryUserWorks(outsourcingGoodsId, page);
	}
}
